package com.jk.service;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jk.dao.CouponDao;
import com.jk.entity.Coupon;
import com.jk.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CouponService {

    @Autowired
    private CouponDao couponDao;

    @Autowired
    private RedisTemplate redisTemplate;


    public List<Coupon> queryCouponById(HttpServletRequest request) throws ParseException {
        User user = (User) request.getSession().getAttribute("user");
        String coupon = (String) redisTemplate.opsForHash().get("coupon", user.getUser_id());
        Gson gson = new Gson();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        List<Coupon> list= gson.fromJson(coupon, new TypeToken<List<Coupon>>() {}.getType());
        return list;
    }

    public Coupon queryById(String couponId) {
        Coupon coupon = couponDao.queryById(couponId);
        if (coupon.getCoupon_type()==1){
            coupon.setCouponTypeName("商品优惠券");
        }if (coupon.getCoupon_type()==2){
            coupon.setCouponTypeName("运费优惠券");
        }
        return coupon;
    }

    public void insertCoupon(HttpServletRequest request, Coupon coupon) {
        User user = (User) request.getSession().getAttribute("user");
        Gson gson = new Gson();
        String couponJson = (String) redisTemplate.opsForHash().get("coupon", user.getUser_id());
        List<Coupon> list= gson.fromJson(couponJson, new TypeToken<List<Coupon>>() {}.getType());
        List<Coupon> coupons = new ArrayList<>();
        Boolean boo= false;
        Date date = new Date();
        coupon.setCoupon_startDate(date.toLocaleString().substring(0,10));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 30);
        coupon.setCoupon_endDate(calendar.getTime().toLocaleString());
        coupon.setCoupon_state(1);
        if (list!=null){
            for (int i = 0; i < list.size(); i++) {
                if (coupon.getCoupon_id().equals(list.get(i).getCoupon_id())){
                    request.getSession().setAttribute("coupon",user.getUser_id()+"已经领取过该优惠券");
                    boo=true;
                }else{
                    coupons.add(list.get(i));
                }
            }
        }
        if (boo==false){
            Map<String, Object> map = new HashMap<String,Object>();
            coupons.add(coupon);
            map.put(user.getUser_id(), gson.toJson(coupons));
            redisTemplate.opsForHash().putAll("coupon",map);
        }
    }


    public void deleteCoupon(HttpServletRequest request, String couponId) {
         User user = (User) request.getSession().getAttribute("user");
         String coupon = (String) redisTemplate.opsForHash().get("coupon", user.getUser_id());
        Gson gson = new Gson();
        List<Coupon> list= gson.fromJson(coupon, new TypeToken<List<Coupon>>() {}.getType());
        for (int i = 0; i < list.size(); i++) {
            if (couponId.equals(list.get(i).getCoupon_id())){
                    list.remove(i);
            }
        }
        redisTemplate.opsForHash().delete("coupon",user.getUser_id());
        Map<String, Object> map = new HashMap<>();
        map.put(user.getUser_id(),gson.toJson(list));
        redisTemplate.opsForHash().putAll("coupon",map);
    }

    public List<Coupon> query() {
        List<Coupon> list = couponDao.query();
        return list;
    }

    public List<Coupon> getCoupon(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        String coupon = (String) redisTemplate.opsForHash().get("coupon", user.getUser_id());
        Gson gson = new Gson();
        List<Coupon> list= gson.fromJson(coupon, new TypeToken<List<Coupon>>() {}.getType());
        return list;
    }
}










